from django.shortcuts import render, HttpResponse, redirect
from django.urls import reverse
from book import models


# Create your views here.
def books(request):
    """
    查询书籍列表
    新的数据类型：QuerySet（可切片[可看做列表]，可循环，可...）
    :param request:
    :return:
    """
    # 查询所有
    book_list = models.Book.objects.all()  # <QuerySet [<Books: Books object (1)>, <Books: Books object (2)>]>
    return render(request, "book_view.html", {"book_list": book_list})


def book_add(request):
    """
    添加图书
    # 使用ORM来实现 create方法，包含了save
    Books.objects.create(title="Shell编程入门", price=111, pub_date="2019-1-1", publish="人民出版社")
    return HttpResponse("OK")
    注意：create方法的返回值是当前生成的表记录的对象
    有返回值的，返回值就是生成的这本书的对象
    book = Books.objects.create(title="Shell编程入门", price=111, pub_date="2019-1-1", publish="人民出版社")
    return HttpResponse("OK")
    :param request:
    :return: 返回当前生成的表记录book表记录对象
    """
    author_list = models.Author.objects.all()
    publish_list = models.Publish.objects.all()
    if request.method == "POST":
        print(request.POST)
        title = request.POST.get('title')
        price = request.POST.get('price')
        pub_date = request.POST.get('pub_date')
        publisher = request.POST.get('publisher')
        authors = request.POST.getlist('authors')
        models.Book.objects.create(title=title, price=price, pub_date=pub_date, publisher=publisher, authors=authors)
        return redirect(reverse("books"))
    else:
        return render(request, "book_add.html", {"author_list": author_list, "publish_list": publish_list})


def book_del(request, delete_book_id):
    pass


#     """
#     删除书籍
#     :param request: 传入要删除书籍的nid
#     :param delete_book_id: 接收要删除书籍的id号
#     :return: 返回是否删除成功
#     """
#     Books.objects.filter(nid=delete_book_id).delete()
#     return redirect(reverse("books"))


def book_edit(request, edit_book_id):
    pass
#     """
#     修改书籍：（get请求拿数据，post请求提交修改后的数据）
#     1、先根据要修改的书籍的id，获取要修改的书籍的对象，传给编辑页面，在前端根据对象用深度查询到各个字段，填充修改书籍页面form表单的默认值
#     2、提交submit，执行修改update
#     3、修改后，保存，和添加书籍一样的处理逻辑，只不过添加书籍是create，这里根据要修改书籍的nid，用update修改
#     :param request: 根据要修改书籍的nid，get该本书的其他属性填充到表单中
#     :param edit_book_id: 要修改书籍的id
#     :return: 返回书籍列表
#     """
#     if request.method == "GET":
#         # 查询编辑对象（先filter到要编辑的对象，然后用update修改掉即可）
#         # Books.objects.filter(nid=edit_book_id).update(price=120)  # http://127.0.0.1:8000/edit/9  就直接修改掉了
#         # return redirect("/books/")
#         # 根据要编辑的书籍的id，查到对应的书籍
#         # edit_book = Books.objects.filter(nid=edit_book_id)[0]
#         # 这里的filter方法也可以换成get方法，用主键查询到的结果一定只有一个，不可能有重复的，因为是编辑，也不可能不存在，所以这里用get方法很完美。
#         edit_book = Books.objects.get(nid=edit_book_id)   # 优化
#         # 返回的是一个QuerySet，要从QuerySet中取到要修改的对象[0],为什么是0，因为根据nid查出来就一个元素，默认就是列表中就一个元素，取的时候就是用[0]
#         return render(request, "editbook.html", {"edit_book": edit_book})
#     else:
#         title = request.POST.get('title')
#         price = request.POST.get('price')
#         pub_date = request.POST.get('pub_date')
#         publish = request.POST.get('publish')
#         # 需要优化的点：改哪个就提交哪个，修改哪个。下面这种无论改不改，改哪个，都全部提交了一遍。
#         Books.objects.filter(nid=edit_book_id).update(title=title, price=price, pub_date=pub_date, publish=publish)
#         return redirect(reverse("books"))
#
#
# #############################################################
# def query(request):
#     """
#     QuerySet 语法
#     :param request:
#     :return:
#     """
#     # # 1 all()  返回 QuerySet
#     # ret = Books.objects.all()  # <QuerySet [<Books: Python>, <Books: Shell>, <Books: GO>, <Books: ELK>]>
#     # print(ret)
#     # # 2 filter 返回 QuerySet
#     # ret = Books.objects.filter(title='linux')  # <QuerySet [<Books: linux>]>
#     # ret = Books.objects.filter(title='linux', price=23)  # <QuerySet [<Books: linux>]>
#     # print(ret)
#     # # 3 get() 返回查询到的 model对象，查到的结果有且只有一个才能正确返回，超过一个或不足一个就报错
#     # ret = Books.objects.get(title='linux')
#     # print(ret.title)   # model对象可以去点它的属性
#     # # 4 first(),last()方法 QuerySet调用，返回model对象
#     # fbook = Books.objects.all()[0]
#     # fbook = Books.objects.all().first()
#     # lbook = Books.objects.all().last()
#     # # 5 exclude() 返回 QuerySet
#     # ret = Books.objects.exclude(price=111)  # <QuerySet [<Books: Python>, <Books: ELK>, <Books: linux>]>
#     # print(ret)
#     # # order_by() 返回 QuerySet
#     # ret = Books.objects.all().order_by("price")
#     # 默认升序<QuerySet [<Books: linux>, <Books: ELK>, <Books: Python>, <Books: Shell>, <Books: GO>]>
#     # ret = Books.objects.all().order_by("-price")
#     # 降序 <QuerySet [<Books: Shell>, <Books: GO>, <Books: Python>, <Books: ELK>, <Books: linux>]>
#     # print(ret)
#
#     return HttpResponse("查询成功！")


# def index(request):
#     return render(request, 'index.html', locals())
#
